
import flash.events.Event;

import mx.controls.Alert;
 
private var c:Object = new Object();


//----
//import org.papervision3d.core.proto.MaterialObject3D;
//--------

private function initKeyFunctions():void {
	stage.addEventListener('keyDown',
		function (e:KeyboardEvent):void {
			switch(e.keyCode){
				case Keyboard.LEFT:
					model.x += modelConfig.settings.zoom_speed;
					//camera.x += movementSpeed;
					break;
				case Keyboard.RIGHT:
					model.x -= modelConfig.settings.zoom_speed;
					//camera.x -= movementSpeed;
					break;
				case Keyboard.UP:
					model.z -= modelConfig.settings.zoom_speed;
					//camera.z += movementSpeed;
					break;
				case Keyboard.DOWN:
					model.z += modelConfig.settings.zoom_speed;
					//camera.z -= movementSpeed;
					break;
				case 87:
					model.rotationX += 5;
					//xz3D.rotationX += 5;
					break;
				case 83:
					model.rotationX -= 5;
					//xz3D.rotationX -= 5;
					break;
				case 65:
					model.rotationY += 5;
					//xz3D.rotationY += 5;
					break;
				case 68:
					model.rotationY -= 5;
					//xz3D.rotationY -= 5;
					break;
				case 82 || 114 : //R
					rotate()
//				case 114: //r
//					setModelRotation()
			}
		});
}



private function zoomIn():void {
    model.z -= modelConfig.settings.zoom_speed;
}

private function zoomOut():void {
    model.z += modelConfig.settings.zoom_speed;
}

private function rotateX():void {
    model.rotationX += modelConfig.settings.rotate_speed;
}

private function rotateY():void {
    model.rotationY += modelConfig.settings.rotate_speed;
}

private function rotateZ():void {
    model.rotationZ += modelConfig.settings.rotate_speed;
}

// this changes the colours of the model when the sample list is selected
private function click_modelList():void {
    var partName:String = list_modelElements.selectedItem.data.toString();
	
//	if (partName == null) {
//		ExternalInterface.call("alert","null");
//	} else if (partName == "") {
//		ExternalInterface.call("alert","ok");
//	}
	// if the part isn't in the list, add it with its colour
	if (!c[partName]){
		c[partName] = model.getChildByName("COLLADA_Scene").getChildByName(partName).material;
	}
	// highlight current part
    paintObject(partName, new ColorMaterial(0x00b400));
	
	// if something was already selected, return it to its original state
    if (currSelectObject) {
		paintObject(currSelectObject, c[currSelectObject]);
    } 
	
    currSelectObject = partName;
	
	// Tissue source
	if (currLevel == "Tissue Level") {
		var source:Array = tissueSources[partName].split("URL");
		lblSourceT1.visible = true;
		lblSourceT1.label = source[0];
		tissueLink = source[1];
	}
}

private function interpolateModel(event:SliderEvent):void {
	var name:Array = event.target.name.split("_");
	switch (name[1]) {
		case "x":
			model.rotationX = event.target.value;
		break;
		case "y":
			model.rotationY = event.target.value;
		break;
		case "z":
			model.rotationZ = event.target.value;
		break;
	}
}

private function changeStatus(stat:String):void {
    status.text = stat;
}

/**
 *  Toggle the rotation function of the model.
 */
private function setModelRotation():void {
    if (buttStopRotating.selected) {
        rotateModel = true;
    } else {
        rotateModel = false;
    }
}

private function rotate():void {
	if (rotating) {
		rotateModel = false;
		rotating = false;
	} else {
		rotateModel = true;
		rotating = true;
	}
}



/**
 *  Change the model zoom location and configuration settings.
 */
private function changeZoomView():void {
    resetLegend();
    addLegendItem(0, 0x888888, "No legend available.");
    for (var i:Object in config.modelmaps) {
    	if (config.modelmaps[i].id == 'plant_level') {
			//Alert.show(event.target.id);
    		serve_getConfigData.url = config.modelmaps[i].path;
        	serve_getConfigData.send();
    	}
    }
}

/**
 *  A function that recurses through all the mapped tissue function and requests
 *  the data from the specified web service. Legend on the left!
 */
private function addLegendItem(i:int, paintColour:uint, label:String):void {
    signalLegend.addChild(new LegendItem());
	//ExternalInterface.call("alert","ok");
    var legendItem:LegendItem = signalLegend.getChildAt(i) as LegendItem;
    legendItem.label = label;
    legendItem.setStyle("fill", paintColour);
	
}
